﻿using System.Web.UI;
using System.Web.UI.WebControls;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Extensions
{
    /// <summary>
    /// Extension methods for the Prevalue Editor
    /// </summary>
    public static class FormExtensions
    {
        /// <summary>
        /// Adds a new row to the Prevalue Editor form.
        /// </summary>
        /// <param name="writer">The HtmlTextWriter.</param>
        /// <param name="label">The label for the field.</param>
        /// <param name="controls">The controls for the field.</param>
        public static void AddFormRow(this HtmlTextWriter writer, string label, params Control[] controls)
        {
            writer.AddFormRow(label, string.Empty, controls);
        }

        /// <summary>
        /// Adds a new row to the Prevalue Editor form, (with an optional description).
        /// </summary>
        /// <param name="writer">The HtmlTextWriter.</param>
        /// <param name="label">The label for the field.</param>
        /// <param name="description">The description for the field.</param>
        /// <param name="controls">The controls for the field.</param>
        public static void AddFormRow(this HtmlTextWriter writer, string label, string description, params Control[] controls)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "propertyItem");
            writer.RenderBeginTag(HtmlTextWriterTag.Div); // start 'row'

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "propertyItemheader");
            writer.RenderBeginTag(HtmlTextWriterTag.Div); // start 'label'

            Label lbl = new Label() { Text = label }; // TODO: Is it possible to include 'AssociatedControlID'?
            lbl.RenderControl(writer);

            if (!string.IsNullOrEmpty(description))
            {
                writer.WriteBreak();

                writer.RenderBeginTag(HtmlTextWriterTag.Small); // start 'description'

                Label desc = new Label() { Text = description };
                desc.RenderControl(writer);

                writer.RenderEndTag(); // end 'description'
            }

            writer.RenderEndTag(); // end 'label'

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "propertyItemContent");
            writer.RenderBeginTag(HtmlTextWriterTag.Div); // start 'field'

            foreach (Control control in controls)
            {
                control.RenderControl(writer);

            }

            writer.RenderEndTag(); // end 'field

            writer.RenderEndTag(); // end 'row'
        }
    }
}